import axios from "../../http/axios.js";
import {
  apiGetUserOrderList,
  apiUpdateOrderStatus
} from "../../http/server.js";
import {
  pay
} from "../../assets/js/pay.js"

Page({
  /**
   * 页面的初始数据
   */
  data: {
    nolist: {
      img: "/assets/img/nolist.png",
      title: "您还没有相关的订单~",
      button: "返回首页 >>"
    },
    orderList: [
      //   {
      //   id: 1,
      //   title: "浙江省人民医院",
      //   state: "待评价",
      //   list: [{
      //       img: "",
      //       name: "打针",
      //       shortDescription: "肌肉注射,皮下注射",
      //       price: "199.0"
      //     },
      //     {
      //       img: "",
      //       name: "打针",
      //       shortDescription: "肌肉注射,皮下注射",
      //       price: "199.0"
      //     }
      //   ],
      //   time: "2019年7月19日 12:00~13:00",
      //   totalPrice: "192.0",
      //   text: "预约号 847285",
      //   button: ["再次预约", "去付款", "评价服务"]
      // }
    ], //订单列表的数据
    tabIdIndex: 0, //顶部tab栏目索引
    tabTop: [
      "全部",
      "待付款",
      "待分配",
      "待服务",
      // "待评价"
    ] //头部的tab
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    console.log("onReady")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let tabIdIndex = wx.getStorageSync("tabIdIndex");
    if (tabIdIndex) {
      this.setData({
        tabIdIndex
      })
    }
    wx.removeStorageSync("tabIdIndex")
    this.getOrderList();
  },
  // 取消订单
  updateOrderStatus(option) {
    axios({
      url: apiUpdateOrderStatus,
      method: "POST",
      data: {
        orderNo: option.orderNo,
        orderStatus: option.orderStatus
      }
    }).then(res => {
      this.getOrderList();
      // let {
      //   tabIdIndex
      // } = this.data;
      // switch (tabIdIndex) {
      //   case 0:
      //     {
      //       break;
      //     }
      //   case 1:
      //     {
      //       break;
      //     }
      //   case 2:
      //     {
      //       this.getOrderList();
      //       break;
      //     }
      //   case 3:
      //     {
      //       break;
      //     }
      // }
    })
  },
  //获取订单
  getOrderList: function() {
    let tabIdIndex = this.data.tabIdIndex;
    // console.log(tabIdIndex);
    let orderType = 0;
    switch (Number(tabIdIndex)) {
      case 0: // 全部
        {
          orderType = 0;
          break;
        }
      case 1: // 待付款
        {
          orderType = 1;
          break;
        }
      case 2: // 待分配
        {
          orderType = 2;
          break;
        }
      case 3: // 待服务
        {
          orderType = 3
          break;
        }
      case 4:
        {
          // 待评价  // 待评价目前暂时在开发
          orderType = 0
          break;
        }
      default:
        {
          break;
        }
    }
    let data = {
      userId: wx.getStorageSync("userInfo").userId,
      pageNum: 1,
      pageSize: 1000,
      orderType,
    }
    axios({
      url: apiGetUserOrderList,
      data: data
    }).then(res => {
      this.processingData(res.records);
    })
  },
  processingData(data) {
    let orderList = [];
    data.forEach(item => {
      function pushList() {
        let listOne = {
          ...item,
          id: item.orderId,
          title: item.hospitalName,
          state: state,
          list: [{
              img: "",
              name: item.serviceName,
              shortDescription: item.serviceDescribe,
              price: Number(item.servicePrice) / 100
            },
            {
              img: "/assets/img/haocaibao.png",
              name: "耗材包 X " + item.consumablesNum,
              shortDescription: item.consumablesName,
              price: Number(item.consumablesPrice) / 100
            }
          ],
          time: "预约时间 " + item.serviceTime,
          totalPrice: Number(item.servicePrice) / 100 + (Number(item.consumablesPrice) / 100) * Number(item.consumablesNum),
          text: text,
          button: buttonList,
        };
        orderList.push(listOne);
      }
      let status = item.status;
      let state = "";
      let buttonList = [];
      let text = "";
      switch (status) {
        case 1:
          {
            state = "待付款";
            buttonList = ["去付款"];
            text = "";
            pushList()
            break;
          }
        case 2:
          {
            state = "待分配";
            buttonList = ["取消订单", "电话咨询"];
            text = "等待医院分配订单";
            pushList();
            break;
          }
        case 3:
          {
            state = "待服务";
            buttonList = ["取消订单", "电话咨询"];
            text = "预约号 " + item.reservationNumber;
            pushList();
            break;
          }
        case 4:
          {
            state = "进行中";
            buttonList = ["取消订单", "电话咨询"];
            text = "";
            break;
          }
        case 5:
          {
            state = "已完成";
            buttonList = ["再次预约", "去评价"];
            pushList()
            break;
          }
        case 6:
          {
            state = "已关闭";
            // buttonList = ["再次预约"]
            pushList()
            break;
          }
        case 7:
          {
            state = "已取消";
            buttonList = ["再次预约"];
            text = "个人取消"
            pushList()
            break;
          }
        case 8:
          {
            state = "已取消";
            buttonList = ["再次预约"]
            text = "医院取消"
            pushList()
            break;
          }
        case 9:
          {
            state = "已退款";
            break;
          }
      }
    })
    this.setData({
      orderList
    })
  },
  // 点击按钮
  clickBtn: function(e) {
    let item = e.detail.item;
    console.log("210item", item);
    // return;
    switch (e.detail.item2) {
      case "再次预约":
        {
          wx.navigateTo({
            url: `/pages/u_detail/u_detail?serviceId=${item.serviceId}&hospitalId=${item.hospitalId}&title=${item.serviceName}`,
          })
          break;
        }
      case "评价详情":
        {
          break;
        }
      case "去付款":
        {
          pay({
            orderNo: item.orderNo
          }).then(res => {
            wx.showToast({
              title: "支付成功",
              icon: "none"
            })
            this.setData({
              tabIdIndex: 2
            })
            this.getOrderList();
          }).catch(err => {
            wx.showToast({
              title: "支付失败",
              icon: "none"
            })
            this.setData({
              tabIdIndex: 1
            })
            this.getOrderList();
          })
          break;
        }
      case "取消订单":
        {
          let option = {
            orderNo: item.orderNo,
            orderStatus: 7
          }
          this.updateOrderStatus(option);
          break;
        }
      case "电话咨询":
        {
          wx.makePhoneCall({
            phoneNumber: item.hospitalPhone // 医院固定的电话号码
          })
          break;
        }
      case "评价服务":
        {
          break;
        }
      default:
        {
          break;
        }
    }
  },
  //tab栏目点击其中的一项
  onTabClickOne: function(e) {
    let obj = e.detail;
    let index = obj.index;
    this.setData({
      tabIdIndex: index
    });
    this.getOrderList();
  }
})